package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Employee;
import com.itheima.excecption.CustomException;
import com.itheima.excecption.NotLocalException;
import com.itheima.tool.R;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @PostMapping("login")
    public R<Employee> Login(HttpServletRequest request, @RequestBody Employee emp) {
        String password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, emp.getUsername());
        Employee EmpByDB = employeeService.getOne(lqw);

        String localAddr = request.getRemoteAddr();

        if (!localAddr.equals("127.0.0.1") && emp.getUsername().equals("admin")) {
            throw new NotLocalException("不是主机不能登录管理员");
        }

        if (EmpByDB == null) {
            return R.error("账户错误");
        }

        if (!EmpByDB.getPassword().equals(password)) {
            return R.error("密码错误");
        }

        if (EmpByDB.getStatus() == 0) {
            return R.error("账户已被封禁");
        }
        request.getSession().setAttribute("employee", EmpByDB.getId());
        return R.success(EmpByDB);
    }

    @PostMapping("/logout")
    public R exit(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success();
    }

    @PostMapping
    public R<Employee> AddEmp(HttpServletRequest request, @RequestBody Employee emp) {
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        emp.setStatus(1);
        employeeService.save(emp);
        return R.success();
    }

    @GetMapping("/page")
    public R GetAllEmpByPage(Integer page, Integer pageSize, String name) {
        Page pageinfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), Employee::getUsername, name);
        lqw.orderByDesc(Employee::getUpdateTime);
        Page pages = employeeService.page(pageinfo, lqw);
        return R.success(pages);
    }

    @PutMapping
    public R updateEmpStatus(@RequestBody Employee emp) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getId, emp.getId());
        if (emp.getUsername() != null && emp.getUsername().equals("admin")) {
            throw new CustomException("不能修改管理员");
        }
        Employee getEmpById = employeeService.getOne(lqw);
        getEmpById.setStatus(emp.getStatus());
        employeeService.updateById(getEmpById);

        return R.success();
    }

    @GetMapping("/{id}")
    public R getEmp(@PathVariable("id") Long id) {
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("未查询到");
    }
}
